package com.yuanfeng.controller;

import com.yuanfeng.basedata.annotation.ConfigManagementSwagger;
import com.yuanfeng.basedata.api.FineFoodConstantApi;
import com.yuanfeng.basedata.entiry.KcsjColumn;
import com.yuanfeng.basedata.entiry.KcsjFood;
import com.yuanfeng.basedata.entiry.KcsjMessage;
import com.yuanfeng.basedata.entiry.KcsjUser;
import com.yuanfeng.basedata.repository.KcsjColumnRepository;
import com.yuanfeng.basedata.response.ResponseResult;
import com.yuanfeng.service.FineFoodService;
import com.yuanfeng.service.MessageManageService;
import com.yuanfeng.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @ClassName UserBrowseController
 * @Description T0D0
 * @Author yuanfeng
 * @Date 2019/9/7 10:14
 * @Version 1.0
 **/
@RequestMapping(FineFoodConstantApi.API_PATH)
@RestController
@Validated
@Api(description = "用户浏览模块")
@ConfigManagementSwagger
public class UserBrowseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserBrowseController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private MessageManageService messageManageService;

    @Autowired
    private KcsjColumnRepository columnRepository;

    @Autowired
    private FineFoodService fineFoodService;

    @ApiOperation(value = "用户端首页详情")
    @RequestMapping(value = "/user_info/detail", method = RequestMethod.GET)
    public ResponseResult<KcsjUser> userInfoDetail(@ApiParam("用户ID") @RequestParam Integer userId) {
        ResponseResult<KcsjUser> responseResult = new ResponseResult<>();
        try {

            Optional<KcsjUser> oneUser = userService.findOneUser(userId);
            //有该用户
            if (oneUser.isPresent() && Objects.nonNull(userId)) {
                responseResult.setCode(200);
                responseResult.setMessage("查询成功！");
                responseResult.setData(oneUser.get());
            } else {
                LOGGER.error("查无此人");
                responseResult.setCode(500);
                responseResult.setMessage("没有该用户，用户首页详情展示失败！");
                responseResult.setData(null);
            }

        } catch (Exception e) {
            LOGGER.error(e + "");
            responseResult.setCode(500);
            responseResult.setMessage("出现异常,用户首页详情展示失败");
            responseResult.setData(null);
        }
        return responseResult;
    }

    @ApiOperation(value = "得到用户所有列名")
    @RequestMapping(value = "/user_info/columns", method = RequestMethod.GET)
    public ResponseResult<List<Map<String, Object>>> getUserColumns(@ApiParam("表ID") @RequestParam String tableId) {

        int selectId = Integer.parseInt(tableId);
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String, Object>> mapList = new ArrayList<>();
        ResponseResult<List<Map<String, Object>>> responseResult = new ResponseResult<>();
        try {

            List<KcsjColumn> kcsjColumns = fineFoodService.getColumnNameDescript(selectId);
            for (KcsjColumn column : kcsjColumns) {
                resultMap.put("key", column.getColumnDescript());
                mapList.add(resultMap);
                resultMap = new HashMap<>();
            }
            //有该用户
            if (Objects.nonNull(kcsjColumns)) {
                responseResult.setCode(200);
                responseResult.setMessage("查询成功！");
                responseResult.setData(mapList);
            } else {
                LOGGER.error("查无此列");
                responseResult.setCode(500);
                responseResult.setMessage("没有该用户列，用户列查询失败！");
                responseResult.setData(null);
            }

        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(e + "");
            responseResult.setCode(500);
            responseResult.setMessage("出现异常,用户列查询失败");
            responseResult.setData(null);
        }
        return responseResult;
    }

    @ApiOperation(value = "美食介绍")
    @RequestMapping(value = "/finefood/introduction", method = RequestMethod.GET)
    public ResponseResult<List<KcsjFood>> fineFoodIntroduction() {
        ResponseResult<List<KcsjFood>> responseResult = new ResponseResult<>();
        //列出所有得美食
        try {
            List<KcsjFood> kcsjFoods = fineFoodService.listFineFoods();

            if (Objects.nonNull(kcsjFoods)) {
                responseResult.setCode(200);
                responseResult.setMessage("美食展示成功");
                responseResult.setData(kcsjFoods);
            }
        } catch (Exception e) {
            responseResult.setCode(500);
            responseResult.setMessage("美食展示失败");
            responseResult.setData(null);
            LOGGER.error(e + "");
            e.printStackTrace();
        }
        return responseResult;
    }

    @ApiOperation(value = "美食详情介绍")
    @RequestMapping(value = "/finefood/details", method = RequestMethod.GET)
    public ResponseResult<KcsjFood> fineFoodDetails(@ApiParam("美食ID") @RequestParam Integer foodId) {
        ResponseResult<KcsjFood> responseResult = new ResponseResult<>();
        //列出所有得美食
        try {
            Optional<KcsjFood> oneFood = fineFoodService.findOneFood(foodId);

            if (oneFood.isPresent()) {
                responseResult.setCode(200);
                responseResult.setMessage("美食详情展示成功");
                responseResult.setData(oneFood.get());
            }
        } catch (Exception e) {
            responseResult.setCode(500);
            responseResult.setMessage("美食详情展示失败");
            responseResult.setData(null);
            LOGGER.error(e + "");
            e.printStackTrace();
        }
        return responseResult;
    }


    @ApiOperation(value = "留言")
    @RequestMapping(value = "/leave/message", method = RequestMethod.POST)
    public ResponseResult<KcsjMessage> leaveMessages(@ApiParam("留言对象") @RequestBody KcsjMessage kcsjMessage) {
        ResponseResult<KcsjMessage> responseResult = new ResponseResult<>();
        try {
            if (Objects.nonNull(kcsjMessage)) {
                KcsjMessage leaveMessage = messageManageService.leaveMessage(kcsjMessage);
                if (leaveMessage != null) {
                    responseResult.setCode(200);
                    responseResult.setMessage("留言成功");
                    responseResult.setData(kcsjMessage);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.warn(e + "");
            responseResult.setCode(500);
            responseResult.setMessage("留言失败");
            responseResult.setData(null);
        }

        return responseResult;
    }

    @ApiOperation(value = "根据ID得到用户所有留言")
    @RequestMapping(value = "/get/user/messages", method = RequestMethod.GET)
    public ResponseResult<List<KcsjMessage>> getUserAllMessages(@ApiParam("用户ID") @RequestParam("userId") String userId) {

        ResponseResult<List<KcsjMessage>> responseResult = new ResponseResult<>();
        try {
            int paramId = Integer.parseInt(userId);
            List<KcsjMessage> kcsjMessages = messageManageService.listAllMessage(paramId);
            if (kcsjMessages.size() > 0) {
                responseResult.setData(kcsjMessages);
                responseResult.setMessage("显示所有留言..");
                responseResult.setCode(200);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            LOGGER.warn(e + "");
            responseResult.setData(null);
            responseResult.setMessage("显示所有留言失败..");
            responseResult.setCode(500);
        }

        return responseResult;
    }

}
